package com.ebupt.migu.music.hangupsms.config;

import com.ebupt.migu.music.hangupsms.entity.MaxNumInfo;
import com.ebupt.migu.music.hangupsms.entity.ProvinceSwitch;
import com.ebupt.migu.music.hangupsms.entity.TimeLimit;
import com.ebupt.migu.music.hangupsms.service.IProvinceSwitchService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "hangupsms")
public class HangUpSmsConfig {
    @Autowired
    private IProvinceSwitchService switchService;
    /**
     * 核心线程池大小
     */
    private int                              asyncCorePoolSize           = 30;
    /**
     * 最大线程大小
     */
    private int                              asyncMaxPoolSize            = 30;

    /**
     * 线程队列大小
     */
    private int                              asyncqueueSize              = 2000;
    private int                              queryBlackUserPageSize      = 500;
    private boolean                          needLog                     = true;
    private boolean                          needCountLog                = true;
    private boolean                          needHandleKafka             = true;
    /**
     * 不发送短信的省
     */
    private String                           noSendSmsProvinceId         = "";
    private String                           callerNoSendGIndex          = "1|4|5|6|9";                                                                  // 主叫不发送短信省份id
    private String                           prefixsToneid               = "6009266";
    private String                           corpToneIds                 = "60|61|62|63|64|65|66|67|68|69";
    /**
     * 控制周、月、天发送短信限制的map（provinceId:info）
     */
    private Map<String, MaxNumInfo>          maxNumInfoMap;
    /**
     * 省份超时时间不发送短信的map
     */
    private Map<String, TimeLimit>           provinceTimeLimitMap;

    /**
     * 短信模板内容对应map
     */
    private Map<String, Map<String, String>> msgContentMap;
    /**
     * 视频彩铃基础会员的serviceId
     */
    private String                           vrbtBaseVipServiceId        = "698039030000000001|698039020108900898|698039020105779117|698039030108485751";
    /**
     * 限流的分省map
     */
    private Map<String, String>              limitCountMap;
    /**
     * 发送短信的端口
     */
    private String                           msgSendPort                 = "1065883099";
    /**
     * redis锁存活时间，单位秒
     */
    private Integer                          checkPhoneNumInterval       = 300;
    /**
     * redi限流的锁存活时间，单位秒
     */
    private Integer                          ratelimitInterval           = 100;
    /**
     * peovinceId和localid 的对应关系
     */
    private Map<String, String>              provinceAndLocalIdMap;
    /**
     * peovinceName和id 的对应关系
     */
    private Map<String, String>              provinceNameAndIdMap;
    /**
     * 需要查询是否是企业的彩铃用户的省份
     */
    private String                           needCheckCorpProvinceIds    = "21";
    /**
     * 调用keypress的did
     */
    private String                           sendDid                     = "1128002";
    /**
     * 调用keypress的didpwd
     */
    private String                           sendDidPwd                  = "Huawei_123";
    /**
     * 休眠时间
     */
    private long                             sleepTimes                  = 3000;
    /**
     * 发送短信白名单
     */
    private String                           whiteMsisdns                = "15228998102";
    /**
     * 发送短信记录需要缓存的手机尾号
     * 如：1，2，3
     * 如：11，12，13
     */
    private List<Integer> sendRecordCacheModeList;
    /**
     * 缓存发送记录的模值
     */
    private int                              sendRecordCacheModeFactor   = 10;
    /**
     * 是否使用缓存开关
     */
    private boolean                          sendRecordCacheSwitch       = false;
    /**
     * 挂机短信记录缓存预热开关
     */
    private boolean                          sendRecordCacheWarmup       = false;
    /**
     * 挂机短信记录只使用缓存
     */
    private boolean                          sendRecordCacheSingle       = false;
    /**
     * 缓存日志打印比例
     */
    private int                              sendRecordCacheLogRatio     = 2;
    /**
     * 是否打断从数据库价值数据
     */
    private boolean                          sendRecordCacheDbInterrupt  = false;
    /**
     * 从DB每次获取的数据量
     */
    private int                              sendRecordCacheDbSize       = 10000;
    /**
     * 每一秒读取DB的频率
     */
    private int                              sendRecordCacheDbRate       = 10;

    private String                           sendRecordCachePrefix       = "hangup_sms_record_set_";

    /**
     * 黑名单批量查询最大数据量
     */
    private int                              blackUserQueryMaxNumber     = 100;
    /**
     * 黑名单批量插入数据最大数量
     */
    private int                              blackUserInsertMaxNumber    = 100;

    /**
     * 分页加载的黑名单数量
     */
    private int                              blackUserLoadSize           = 10000;
    private boolean                          blackUserCacheSwitchOn      = false;
    private int                              blackUserCacheExpire        = 25 * 3600;
    private int                              blackUserCacheRetry         = 10;
    private String                           warningPhones               = "18884552850|15228998120|18200280062";
    /**
     * 铃音类型
     */
    private String                           toneType                    = "1|";
    /**
     * 铃音id校验位数
     */
    private int                              checkPos                    = 7;
    /**
     * FLink kafka topic
     */
    private String                           producerTopic               = "aiops_flink_person_huadan_rule";
    /**
     * 视频彩铃观看：FLink kafka刷新配置缓存 topic
     */
    private String                           viewVrbtSettingRefreshTopic;
    /**
     * 视频彩铃观看：满足条件发短信后通知宣发topic
     */
    private String                           viewVrbtNotifyXFTopic;
    /**
     * 视频彩铃观看：是否需要处理kafka消息
     */
    private boolean                          viewVrbtNeedHandleKafka     = true;
    /**
     * 视频彩铃观看：不处理时，是否打印日志
     */
    private boolean                          viewVrbtNeedLog             = false;
    /**
     * 视频彩铃观看：默认缓存2小时
     */
    private Integer                          viewVrbtSettingCacheSeconds = 7200;
    /**
     * 视频彩铃观看：已发送短信记录缓存时间，默认3个月
     */
    private Integer                          viewVrbtHasSendCacheSeconds = 7776000;
    /**
     * 视频彩铃观看：创蓝发送短信记录缓存时间，默认3天
     */
    private Integer                          viewVrbtCLSendCacheSeconds  = 259200;
    /**
     * 视频彩铃观看：可发送短信时间段
     */
    private String                           viewVrbtCanSendMsgTimeLimit = "08:00:00|20:00:00";
    /**
     * 删除黑名单用户，默认reids缓存7天
     */
    private Integer                          delBlackUserCacheSeconds    = 604800;
    /**
     * 创蓝短信平台配置
     */
  //  private CLSmsSetting                     clSmsSetting;
    /**
     * 通过挂机短信切换到策略师的比例 1-100对应1%到100%
     */
    private int                              ceLueShiSendRatio           = 0;
    /**
     * 主叫不发送短信省份id
     */
    private String                           callerNoSendProvinceIds  = "12|13";

    @PostConstruct
    public void initSwitch() {
        List<ProvinceSwitch> ProvinceSwitchList = null;
        try {
            ProvinceSwitchList =  switchService.list();
        } catch (Exception e) {
            log.error( "get data error");
        }
        if(CollectionUtils.isEmpty(ProvinceSwitchList)){
            return;
        }
        StringBuilder noSendSmsProvinceId = new StringBuilder();
        for(ProvinceSwitch provinceSwitch: ProvinceSwitchList){
            if (provinceSwitch.getStatus() == 0) {
                noSendSmsProvinceId.append(provinceSwitch.getProvinceCode()).append("|");
            }
        }
        setNoSendSmsProvinceId(noSendSmsProvinceId.toString());
    }

}

